//
//  LYAssesmentViewController.m
//  artist
//
//  Created by Jay_Apple on 16/4/11.
//  Copyright © 2016年 viviyoo. All rights reserved.
//

#import "LYAssesmentViewController.h"
#import "LYVideoModel.h"

@interface LYAssesmentViewController ()<UITextViewDelegate>

@property (weak, nonatomic) IBOutlet UIImageView *icon;

@property (weak, nonatomic) IBOutlet UILabel *name;

@property (weak, nonatomic) IBOutlet UIView *starView;

@property (weak, nonatomic) IBOutlet UITextView *textView;

@property (weak, nonatomic) IBOutlet UIView *seperator;
@property (weak, nonatomic) IBOutlet UILabel *placeHolderText;
@property (strong, nonatomic) LYSubjectDetailModel *subModel;
@property (strong, nonatomic) LYVideoDetailModel *videoModel;

@property (nonatomic, assign) NSUInteger star;

@end

@implementation LYAssesmentViewController


- (void)awakeFromNib{


}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    if (self.hideComponent) {
        self.icon.hidden = YES;
        self.seperator.hidden = YES;
        self.name.hidden = YES;
    }
    
    if (self.placeHolder) {
        self.placeHolderText.text = self.placeHolder;
    }
    
    //添加手势
    UITapGestureRecognizer *pan = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
    [self.starView addGestureRecognizer:pan];
    
    self.star = 0;
    
    self.navigationItem.title = @"评价";
    
    [self getCommentData];

}

#pragma mark - 重写
- (void)setStar:(NSUInteger)star{

    _star = star;
    
    for (NSInteger i = 0; i < 5; i++) {
        
        UIImageView *imageV = self.starView.subviews[i];
        if (i < _star) {
            imageV.image = [UIImage imageNamed:@"星星选中"];
        }else{
            imageV.image = [UIImage imageNamed:@"星星未选中"];
        }
        
    }


}

- (void)setSubModel:(LYSubjectDetailModel *)subModel{


    _subModel = subModel;
    
    [self.icon sd_setImageWithURL:[NSURL URLWithString:_subModel.head_pic]];
    self.name.text = _subModel.title;

}

#pragma mark - 网络请求

- (void)getCommentData{

    switch (self.type) {
        case CommentTypeClass:{
            
            [LYRequestTool getSubjectDeatilWithToken:LYToken course_id:self.ID success:^(LYSubjectDetailModel *model) {
                
                self.subModel = model;

                
            } failure:^(NSString *code, NSString *message) {
                [MBProgressHUD showOnlyText:message];
            }];
    }
            break;
            
        case CommentTypeVideo:{
            
            [LYRequestTool getVideoDeatilWithToken:LYToken video_id:self.ID success:^(LYVideoDetailModel *model) {
                
                self.videoModel = model;
                
            } failure:^(NSString *code, NSString *message) {
                [MBProgressHUD showOnlyText:message];
            }];
        }
            
            break;
            
        default:
            break;
    }
    

}


- (void)commentWithToUserID:(NSString *)to_user_id{
    
    [MBProgressHUD showMessage:nil];
    [LYRequestTool sendCommentWithToken:LYToken type:self.type to_user_id:to_user_id data_id:self.ID content:self.textView.text star:self.star order_sn:self.order_sn success:^{
        [MBProgressHUD hideHUD];
        [MBProgressHUD showSuccess:@"评论成功"];
        
        [self.navigationController popViewControllerAnimated:YES];
    } failure:^(NSString *code, NSString *message) {
        [MBProgressHUD hideHUD];
        [MBProgressHUD showOnlyText:message];
    }];


}



#pragma mark - 触摸手势
- (void)pan:(UITapGestureRecognizer *)pan{

    CGPoint point = [pan locationInView:pan.view];
    
    CGFloat starW = self.starView.My_Width / self.starView.subviews.count;
    self.star = point.x / starW + 1;
    
}


#pragma mark - 按钮点击
- (IBAction)commitComment:(id)sender {
    
    NSString *user_id = nil;
    
    switch (self.type) {
        case CommentTypeClass:
            user_id = self.subModel.user_id;
            
            break;
            
        case CommentTypeVideo:
            user_id = self.videoModel.user_id;
            break;
            
            default:
            break;
    }
    
    [self commentWithToUserID:user_id];
    
}



#pragma mark - textViewDelegate


- (void)textViewDidChangeSelection:(UITextView *)textView{
    
    if (textView.text.length == 0) {
        self.placeHolderText.hidden = NO;
    }else{
        self.placeHolderText.hidden = YES;
    }
}
@end
